package edu.ccut.saturn.studio.wfg.command;

import org.eclipse.gef.commands.Command;
import org.eclipse.jface.dialogs.MessageDialog;

import cn.edu.ccut.refactor.Messages;

import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;
import edu.ccut.saturn.studio.wfg.model.Shape;

public class RenameCommand extends Command {

	private Shape shape;

	private String newName;

	private String oldName;

	public void setName(String name) {
		this.newName = name;
	}

	public void setShape(Shape shape) {
		this.shape = shape;
	}

	public void execute() {
		oldName = this.shape.getName();
//		this.shape.setName(newName);
//		oldName = this.shape.getName();
		if (SaturnValidation.isEmpty(newName)) {
			MessageDialog.openInformation(null, edu.ccut.saturn.studio.core.util.eclipse.Messages.getString("RenameCommand.remend"), edu.ccut.saturn.studio.core.util.eclipse.Messages.getString("RenameCommand.NameCannotBeEmpty"));  //$NON-NLS-1$ //$NON-NLS-2$
			this.shape.setName(oldName);
		}else if (SaturnValidation.isBadWord(newName)) {
			MessageDialog.openInformation(null, edu.ccut.saturn.studio.core.util.eclipse.Messages.getString("RenameCommand.remend"), edu.ccut.saturn.studio.core.util.eclipse.Messages.getString("RenameCommand.NameIlegal"));  //$NON-NLS-1$ //$NON-NLS-2$
			this.shape.setName(oldName);
		} else {
			this.shape.setName(newName);
		}
	}

	public void redo() {
		shape.setName(newName);
	}

	public void undo() {
		shape.setName(oldName);
	}
}
